var _gaq = _gaq || [];
_gaq.push( [ '_setAccount', 'UA-1390959-7' ]);
_gaq.push( [ '_trackPageview' ]);
(function() {
   var ga = document.createElement('script');
   ga.type = 'text/javascript';
   ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl'
         : 'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0];
   s.parentNode.insertBefore(ga, s);
})();

function getMapProvider() {
   var a_elem = location.href;
   var mapProvider = null;
   if (a_elem.indexOf('google-maps-mobile') > 0) {
      mapProvider = 'google-maps-mobile';
   } else if (a_elem.indexOf('google-maps') > 0) {
      mapProvider = 'google-maps';
   } else if (a_elem.indexOf('yandex-maps') > 0) {
      mapProvider = 'yandex-maps';
   } else if (a_elem.indexOf('visicom-maps') > 0) {
      mapProvider = 'visicom-maps';
   } else {
      mapProvider = 'google-maps';
   }
   return mapProvider;
}

(function() {

   function getScript(src) {
      document.write('<' + 'script src="' + src + '"'
            + ' type="text/javascript"><' + '/script>');
   }

   getScript("js/maps-providers/" + getMapProvider() + ".js");
})();

//map and marker variables 
var map;
var marker;
var gps_coords;

// init params
var def_lat = 48.50;
var def_lng = 31.40;
var def_zoom = 6;

// pan offset for mobile maps
var panOffset = 100;

// gps params
var gps_lat = null;
var gps_lng = null;
var gps_defzoom = '15';
var gps_defmaptype = 'sat';

// standard map sizes
var sizeMicro_w = '320px';
var sizeMicro_h = '200px';

var sizeA_w = '500px';
var sizeA_h = '300px';

var sizeB_w = '640px';
var sizeB_h = '360px';

var sizeC_w = '854px';
var sizeC_h = '480px';

// default maps size
var def_w = sizeB_w;
var def_h = sizeB_h;

// current map position
var curr_maplat;
var curr_maplng;
var curr_mapzoom;
var curr_maptype;

function getParameter(name) {
   
   var winloc_href = window.location.href;
   if (winloc_href.indexOf('?') == -1 && winloc_href.indexOf('#') == -1) 
      return null;
   
   name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
   var regexS = "[\\?&]" + name + "=([^&#]*)";
   var regex = new RegExp(regexS);
   
   // if the parameter in the url hash
   if (winloc_href.indexOf('#') != -1) {
      var hash_href =  "?" + winloc_href.substring(winloc_href.indexOf('#') + 1, winloc_href.length);

      var results2 = regex.exec(hash_href);
      if (results2 != null) {
         // if find - return
         return results2[1];
      }
   }
   // if the parameter in the url query
   if (winloc_href.indexOf('?') != -1) {
      var hash_position = (winloc_href.indexOf('#') == -1) ? winloc_href.length : winloc_href.indexOf('#');
      var params_href = winloc_href.substring(winloc_href.indexOf('?'), hash_position);
   
      var results = regex.exec(params_href);
      if (results != null) {
         // if find - return
         return results[1];
      }
   }
   return null;
}

function fillUrlsCommon(maplat, maplng, mapzoom, maptype, markerlat, markerlng) {
   // fill the document variable fields for current map position
   document.curr_maplat = maplat;
   document.curr_maplng = maplng;
   document.curr_mapzoom = mapzoom;
   document.curr_maptype = maptype;
   
   var params = 'lat=' + maplat + '&lng=' + maplng + '&z=' + mapzoom + '&t='
   + maptype;
   if (isMarkerVisible()) {
      params += '&mlat=' + markerlat + '&mlng=' + markerlng;
   }
   params += '&w=' + document.getElementById("map_canvas").style.width;
   params += '&h=' + document.getElementById("map_canvas").style.height;
   fillTheUrl(params, 'google-maps');
   fillTheUrl(params, 'google-maps-mobile');
   fillTheUrl(params, 'yandex-maps');
   fillTheUrl(params, 'visicom-maps');
   
   fillCurrentMapPositionInText();
   fillThePageHash(params);
}

function fillCurrentMapPositionInText() {
   var a_elem = location.href;
   var currentLink = null;
   if (a_elem.indexOf('google-maps-mobile') > 0) {
      currentLink = document.getElementById('google-maps-mobile').href;
   } else if (a_elem.indexOf('google-maps') > 0) {
      currentLink = document.getElementById('google-maps').href;
   } else if (a_elem.indexOf('yandex-maps') > 0) {
      currentLink = document.getElementById('yandex-maps').href;
   } else if (a_elem.indexOf('visicom-maps') > 0) {
      currentLink = document.getElementById('visicom-maps').href;
   } else {
      // default
      alert('There is no Map Service in the page location.');
      return;
   }
   document.getElementById('textlink').value = currentLink;
   document.getElementById('refresh').href = currentLink;
}

function indexPageOnLoad() {
   fillIndexPageUrlsWithGPS();
}

function fillIndexPageUrlsWithGPS() {
   if (navigator.geolocation) {
      navigator.geolocation
            .getCurrentPosition(function(position) {
               gps_lat = position.coords.latitude;
               gps_lng = position.coords.longitude;
               document.getElementById("latitude").innerHTML = gps_lat;
               document.getElementById("longitude").innerHTML = gps_lng;
   
               var maplat = gps_lat;
               var maplng = gps_lng;
               var mapzoom = gps_defzoom;
               var maptype = gps_defmaptype;
               var markerlat = gps_lat;
               var markerlng = gps_lng;
               
               var params = 'lat=' + maplat + '&lng=' + maplng + '&z=' + mapzoom + '&t='
                     + maptype;
                  params += '&mlat=' + markerlat + '&mlng=' + markerlng;
               params += '&w=' + def_w;
               params += '&h=' + def_h;
               fillTheUrl(params, 'google-maps');
               fillTheUrl(params, 'yandex-maps');
               fillTheUrl(params, 'visicom-maps');
               fillTheUrl(params, 'google-maps-mobile');
            });
   }
}

function fillTheUrl(params, urlname) {
   var a_elem = document.getElementById(urlname);
   if (a_elem.href.indexOf('#') > 0) {
      a_elem.href = a_elem.href.substring(0, a_elem.href.indexOf('#'))
   }
   if (a_elem.href.indexOf('?') > 0) {
      a_elem.href = a_elem.href.substring(0, a_elem.href.indexOf('?'))
   }
   a_elem.href = a_elem.href + '#' + params;
}

function fillThePageHash(params) {
   document.location.hash = '#' + params;
}

function markerButtons(isSetMarker) {
   if (isSetMarker) {
      document.getElementById("setmarker").style.display = "none";
      document.getElementById("removemarker").style.display = "block";
   } else {
      if (document.getElementById("setmarker").style.display == "none") {
         document.getElementById("setmarker").style.display = "block";
         document.getElementById("removemarker").style.display = "none";
      } else {
         document.getElementById("setmarker").style.display = "none";
         document.getElementById("removemarker").style.display = "block";
      }
   }
}

function pageOnLoad() {
   initGPS();
   // we can add something to init here
}

function initMarkerButtons(visibleMarker) {
   if (visibleMarker) {
      document.getElementById("setmarker").style.display = "none";
      document.getElementById("removemarker").style.display = "block";
   } else {
      document.getElementById("setmarker").style.display = "block";
      document.getElementById("removemarker").style.display = "none";
   }
}

function resizeMapCanvas(w, h) {
   document.getElementById("map_canvas").style.width = w;
   document.getElementById("map_canvas").style.height = h;

   document.getElementById("div_url").style.width = w;
   document.getElementById("map_links").style.width = w;
   document.getElementById("marker_links").style.width = w;
}

function initMapCanvasSize() {
   var w = getParameter('w');
   var h = getParameter('h');
   if (w != null || h != null) {
      if (w == null)
         w = def_w;
      if (h == null)
         h = def_h;
      resizeMapCanvas(w, h);
   } else {
      resizeMapCanvas(def_w, def_h);
   }
}

function viewPort() {
   var h = window.innerHeight || document.documentElement.clientHeight
         || document.getElementsByTagName('body')[0].clientHeight;
   var w = window.innerWidth || document.documentElement.clientWidth
         || document.getElementsByTagName('body')[0].clientWidth;

   return {
      width : w,
      height : h
   }
}

function resizeMapMicro() {
   resizeMapCanvas(sizeMicro_w, sizeMicro_h);
   resizeMap();
}

function resizeMapA() {
   resizeMapCanvas(sizeA_w, sizeA_h);
   resizeMap();
}

function resizeMapB() {
   resizeMapCanvas(sizeB_w, sizeB_h);
   resizeMap();
}

function resizeMapC() {
   resizeMapCanvas(sizeC_w, sizeC_h);
   resizeMap();
}

function resizeMapMacro() {
   resizeMapCanvas(viewPort().width + 'px', viewPort().height + 'px');
   resizeMap();
}

function initGPS() {
   if (navigator.geolocation) {
      navigator.geolocation
            .getCurrentPosition(function(position) {
               gps_lat = position.coords.latitude;
               gps_lng = position.coords.longitude;
            });
   }
}
